package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.base.response.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.DTO.InsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/users/")
@Api(tags = "01.用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("insert")
    @ApiOperation(value = "插入用户功能")
    public JsonResult insert(InsertDTO insertDTO){
        User user = new User();
        BeanUtils.copyProperties(insertDTO,user);
        user.setCreated(new Date());
        log.debug("user="+user);
        userMapper.insertUser(user);

        return JsonResult.ok();
    }
    @GetMapping("select")
    @ApiOperation(value = "查询用户列表")
    public JsonResult selectUserList(){
        List<UserListVO> userListVOS = userMapper.selectUserList();
        return JsonResult.ok(userListVOS);
    }
    @GetMapping("delete")
    @ApiOperation(value = "删除用户")

    public JsonResult deleteUserById(Long id){
        userMapper.deleteUserById(id);
        return JsonResult.ok();
    }
    @PostMapping("update")
    @ApiOperation(value = "修改用户")
    public JsonResult updateUserById(User user){

        userMapper.updateUserById(user);
        log.debug("user="+user);
        return JsonResult.ok();
    }

    /*
    * @PathVariable 注解：
    * 声明路径参数，在路径PATH中获取参数*/
    @GetMapping("selectById/{uid}")
    @ApiOperation(value = "查询指定用户")
    @ApiImplicitParam(name="uid",value ="用户id",required = true,dataType = "int")
    public JsonResult selectById(@PathVariable Integer uid){
        log.debug("uid="+uid);
        UserVO userVO = userMapper.selectById(uid);
        return JsonResult.ok(uid);

    }

}
